/* ESP32C3 Flash Driver Header */

#ifndef __FLASH_H__
#define __FLASH_H__

#include <stdint.h>
#include <stddef.h>

/* Flash sector size */
#define FLASH_SECTOR_SIZE (4 * 1024)  /* 4KB */
#define FLASH_BLOCK_SIZE  (64 * 1024) /* 64KB */
#define FLASH_PAGE_SIZE   (256)       /* 256 bytes */

/* Flash chip size */
#define FLASH_CHIP_SIZE   (4 * 1024 * 1024) /* 4MB */

/* Flash partition types */
typedef enum {
    FLASH_PART_TYPE_APP = 0,
    FLASH_PART_TYPE_DATA,
    FLASH_PART_TYPE_MAX
} flash_part_type_t;

/* Flash partition subtype */
typedef enum {
    /* App subtypes */
    FLASH_PART_SUBTYPE_APP_FACTORY = 0,
    FLASH_PART_SUBTYPE_APP_OTA_0 = 1,
    FLASH_PART_SUBTYPE_APP_OTA_1 = 2,
    FLASH_PART_SUBTYPE_APP_OTA_2 = 3,
    FLASH_PART_SUBTYPE_APP_OTA_3 = 4,
    
    /* Data subtypes */
    FLASH_PART_SUBTYPE_DATA_OTA = 0,
    FLASH_PART_SUBTYPE_DATA_PHY = 1,
    FLASH_PART_SUBTYPE_DATA_NVS = 2,
    FLASH_PART_SUBTYPE_DATA_FAT = 3,
    FLASH_PART_SUBTYPE_DATA_SPIFFS = 4
} flash_part_subtype_t;

/* Flash partition */
typedef struct {
    uint32_t offset;        /* Partition offset in Flash */
    uint32_t size;          /* Partition size */
    flash_part_type_t type; /* Partition type */
    flash_part_subtype_t subtype; /* Partition subtype */
    char label[16];         /* Partition label */
} flash_partition_t;

/* Flash functions */
void flash_init(void);
int flash_erase_sector(uint32_t sector_num);
int flash_erase_block(uint32_t block_num);
int flash_write_page(uint32_t addr, const uint8_t *data, size_t size);
int flash_read(uint32_t addr, uint8_t *data, size_t size);
const flash_partition_t *flash_get_partition(const char *label);

#endif /* __FLASH_H__ */
